package cn.rkylin.oms.order.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.order.domain.EcTrade;

/**
 * 
 * @author Administrator
 *
 */
@Service("taobaoTradeService")
public class TaobaoTradeService {

    @Resource(name = "redisTemplate")
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    @Autowired
    private IDataBaseFactory dao;

    public int update(String updateStatement, EcTrade ecTrade) throws Exception {

        ecTrade.setUpdateTime(new Date());
        int result = dao.update(updateStatement, ecTrade);
        // redis 更新 按照交易号放置
        /*if (result != 0) {
            updateRedis(ecTrade);
        }*/
        return result;
    }

    public int insert(String pageStatement, EcTrade ecTrade) throws Exception {
        int result = dao.insert(pageStatement, ecTrade);
        // redis 更新
        /*if (result != 0) {
            updateRedis(ecTrade);
        }*/
        return result;
    }

    private void updateRedis(EcTrade ecTrade) throws Exception {

        List<String> list = null;
        // redis 更新
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();

        list = (ArrayList<String>) valueops.get("淘宝-Trade-EcTradeCode:" + ecTrade.getEcTradeCode().split("-")[0]);
        if (list != null && list.size() > 0) {
            boolean flg = false;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).equals(ecTrade.getEcTradeId())) {
                    flg = true;
                    break;
                }
            }
            if (!flg) {
                list.add(ecTrade.getEcTradeId());
            }
        } else {
            list = new ArrayList<String>();
            list.add(ecTrade.getEcTradeId());
        }
        valueops.set("淘宝-Trade-EcTradeCode:" + ecTrade.getEcTradeCode().split("-")[0], (Serializable) list);

        // redis shopid是key，内容是交易号
        List<String> listShopKye = null;
        EcTrade parentTrade = (EcTrade) dao.find("selectByPrimaryKeyEcTrade", ecTrade.getParentTradeId());
        listShopKye = (ArrayList<String>) valueops.get("淘宝-Trade-shopId:" + parentTrade.getShopId());
        if (listShopKye == null) {
            listShopKye = new ArrayList<String>();
        }
        boolean flg = false;
        for (int i = 0; i < listShopKye.size(); i++) {
            if (listShopKye.get(i).equals(ecTrade.getEcTradeCode().split("-")[0])) {
                flg = true;
                break;
            }
        }
        if (!flg) {
            listShopKye.add(ecTrade.getEcTradeCode().split("-")[0]);
        }
        valueops.set("淘宝-Trade-shopId:" + parentTrade.getShopId(), (Serializable) listShopKye);

    }

    /**
     * 根据交易号取得分单的EcTrade
     * 
     * @param ecTradeCode
     * @return
     * @throws Exception
     */
    public List<EcTrade> getTradeByCode(String ecTradeCode) throws Exception {
        boolean flg = false;
        EcTrade ecTrade = null;
        List<String> list = null;
        List<EcTrade> listTrade = new ArrayList<EcTrade>();
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();

        list = (ArrayList<String>) valueops.get("淘宝-Trade-EcTradeCode:" + ecTradeCode);
        if (list == null) {
            list = new ArrayList<String>();
        }
        for (int i = 0; i < list.size(); i++) {
            // TODO 性能改造，这里面不用取出整个订单，只要取出1个id列就行了，直接返回id列的ArrayList，直接使用
            ecTrade = (EcTrade) dao.find("selectByPrimaryKeyEcTrade", list.get(i));
            if (ecTrade != null) {
                listTrade.add(ecTrade);
            }
        }

        if (listTrade == null || listTrade.size() <= 0) {
            Map<String, String> reqMap = new HashMap<String, String>();
            reqMap.put("tid", ecTradeCode);
            listTrade = dao.findForList("selectByEcTradeCodeEcTrade", reqMap);
            if (listTrade != null && listTrade.size() > 0) {
                for (int i = 0; i < listTrade.size(); i++) {
                    list.add(listTrade.get(i).getEcTradeId());
                }
                valueops.set("淘宝-Trade-EcTradeCode:" + ecTradeCode, (Serializable) list);
            }
        }

        if (listTrade != null && listTrade.size() > 0) {
            // redis shopid是key，内容是交易号
            List<String> listShopKye = null;
            EcTrade parentTrade = (EcTrade) dao.find("selectByPrimaryKeyEcTrade", listTrade.get(0).getParentTradeId());
            listShopKye = (ArrayList<String>) valueops.get("淘宝-Trade-shopId:" + parentTrade.getShopId());
            if (listShopKye == null) {
                listShopKye = new ArrayList<String>();
            }
            flg = false;
            for (int i = 0; i < listShopKye.size(); i++) {
                if (listShopKye.get(i).equals(ecTradeCode)) {
                    flg = true;
                    break;
                }
            }
            if (!flg) {
                listShopKye.add(ecTradeCode);
            }
            valueops.set("淘宝-Trade-shopId:" + parentTrade.getShopId(), (Serializable) listShopKye);

        }

        return listTrade;
    }

    /**
     * 根据店铺取得所有的trade表的主键
     * 
     * @param shopId
     * @return
     * @throws Exception
     */
    public List<EcTrade> getSplitTradeByShop(String shopId) throws Exception {
        List<EcTrade> listTrade = new ArrayList<EcTrade>();
        ValueOperations<Serializable, Serializable> valueops = redisTemplate.opsForValue();
        List<String> listTradeCode = (ArrayList<String>) valueops.get("淘宝-Trade-shopId:" + shopId);

        if (listTradeCode == null || listTradeCode.size() <= 0) {
            Map<String, String> reqMap = new HashMap<String, String>();
            reqMap.put("shopId", shopId);
            List<EcTrade> listEcTrade = dao.findForList("selectByShop", reqMap);
            listTradeCode = new ArrayList<String>();
            if (listEcTrade != null && listEcTrade.size() > 0) {
                for (int i = 0; i < listEcTrade.size(); i++) {
                    listTradeCode.add(listEcTrade.get(i).getEcTradeCode());
                }
            }
        }

        for (int i = listTradeCode.size(); i > 0; i--) {
            if (listTradeCode.get(i - 1) == null) {
                listTradeCode.remove(i - 1);
                continue;
            }
            List<EcTrade> list = getTradeByCode(listTradeCode.get(i - 1));
            if (list != null && list.size() > 0) {
                for (int j = list.size(); j > 0; j--) {
                    if (!list.get(j - 1).getEcTradeId().equals(list.get(j - 1).getParentTradeId())) {
                        listTrade.add(list.get(j - 1));
                    }
                }
            }
        }
        valueops.set("淘宝-Trade-shopId:" + shopId, (Serializable) listTradeCode);

        return listTrade;
    }
}
